package agh.io.xmlCommunicator {
	import agh.io.api.MapAdapter;
	import flash.display.Sprite;
	import flash.external.ExternalInterface;
	import mx.rpc.soap.LoadEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.AbstractOperation;
	import mx.rpc.soap.WebService;

	/**
	 * @author endq
	 */
	public class Communicator extends Sprite{
		
		protected var webService : WebService;
		
		protected var serviceOperation : AbstractOperation;
		
		protected var jsCallback:String = "";
	
		//public static var SERVER_INTERFACE_URL:String = "http://testphp.endq.eu/io/server/ServerInterface.php?wsdl";
		public static var SERVER_INTERFACE_URL:String;// = "http://testphp.endq.eu/io/server/ServerInterface.php?wsdl";
		
		protected var adapter : MapAdapter;
		
		public function Communicator(a:MapAdapter) {	
			adapter = a;
		}
		
		public function SetupWebService() : void {
			webService = new WebService();
			webService.loadWSDL(SERVER_INTERFACE_URL);			
			webService.addEventListener(LoadEvent.LOAD, BuildServiceRequest);
		} 

		public function BuildServiceRequest(event : LoadEvent) : void {
		} 
		
		public function GetTag() : void {
			serviceOperation = webService.getOperation("getTag");
			
			serviceOperation.addEventListener(FaultEvent.FAULT, DisplayError);
			serviceOperation.addEventListener(ResultEvent.RESULT, DisplayResult);
			
			jsCallback = "callbackTag";
			
			serviceOperation.send();
		}

		public function Search(text:String, valueMin:String, valueMax:String) : void {
			serviceOperation = webService.getOperation("search");
			
			serviceOperation.addEventListener(FaultEvent.FAULT, DisplayError);
			serviceOperation.addEventListener(ResultEvent.RESULT, DisplayResult);
			
			jsCallback = "callbackSearch";
			
			serviceOperation.send(text, parseFloat(valueMin), parseFloat(valueMax));
		}

		public function DisplayError(event : FaultEvent) : void {
			serviceOperation.removeEventListener(FaultEvent.FAULT, DisplayError);
			MapAdapter.status.text = event.toString();
		} 

		public function DisplayResult(event : ResultEvent) : void {	
			serviceOperation.removeEventListener(ResultEvent.RESULT, DisplayResult);	
			
			var data : String = event.result as String;

			var resultString:String = XmlParser.ReplaceAll("&gt;", ">", XmlParser.ReplaceAll("&lt;", "<", data));
			
			adapter.displaySearch(XmlParser.ParseResponse(resultString));
			
			ExternalInterface.call(jsCallback, resultString);
		} 
		
		public function InsertEvent(eventLocationName:String, eventLocationAdres:String, eventLocationCity:String, eventLocationLocationName:String, eventLocationDescription:String, locationX:String, locationY:String, eventName:String, eventDescription:String, eventValue:String, user:String, tags:String, categores:String) : void {
			serviceOperation = webService.getOperation("insertEventLocation");
			
			serviceOperation.addEventListener(FaultEvent.FAULT, DisplayError);
			serviceOperation.addEventListener(ResultEvent.RESULT, InsertEventResult);
			
			jsCallback = "callbackInsertEvent";
			
			serviceOperation.send(eventLocationName, eventLocationAdres, eventLocationCity, eventLocationLocationName, eventLocationDescription, locationX, locationY, eventName, eventDescription, eventValue, user, tags, categores);
		}
		
		public function InsertOnlyEvent(eventLocationId:String, eventName:String, eventDescription:String, eventValue:String, user:String, tags:String, categores:String) : void {
			serviceOperation = webService.getOperation("insertEvent");
			
			serviceOperation.addEventListener(FaultEvent.FAULT, DisplayError);
			serviceOperation.addEventListener(ResultEvent.RESULT, InsertEventResult);
			
			jsCallback = "callbackInsertEvent";
			
			serviceOperation.send(eventLocationId, eventName, eventDescription, eventValue, user, tags, categores);
		}

		public function InsertEventResult(event : ResultEvent) : void {	
			serviceOperation.removeEventListener(ResultEvent.RESULT, InsertEventResult);	
			
			var data : String = event.result as String;
			
			ExternalInterface.call(jsCallback, data);
		} 	
		
	}
}